package cn.gok.tool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Jdbc {
    //根据自己数据库的数据来填
    private static  String URL="jdbc:mysql://localhost:3306/warehouse";
    private static  String DRIVER="com.mysql.cj.jdbc.Driver";
    private static  String user_Name="goket1009";
    private static  String user_PSW="root";
    //静态代码块，如果有需要就加载驱动包
    static {
        try {
            //虽然目前大多数驱动包都会自动加载，但是还是有一部分加载不到
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //操作数据库第一步，建立数据库链接
    public static final Connection getDBConnection() throws SQLException {
        return DriverManager.getConnection(URL, user_Name, user_PSW);
    }

    //写一个关闭操作，不用时把相关接口关闭
    public static final void close(Connection conn,PreparedStatement pstm,ResultSet rs) {
        //关闭资源，因为开启是从前往后开启，所以关闭应该从子项目关闭
        close(rs);
        close(pstm);
        close(conn);
    }
    //重写方法，实现逐个关闭的效果
    public static final void close(ResultSet rs) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static final void close(PreparedStatement pstm) {
        if(pstm!=null) {
            try {
                pstm.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static final void close(Connection conn) {
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
//前端给链接参数
    public static String getURL() {
        return URL;
    }

    public static void setURL(String URL) {
        Jdbc.URL = URL;
    }

    public static String getDRIVER() {
        return DRIVER;
    }

    public static void setDRIVER(String DRIVER) {
        Jdbc.DRIVER = DRIVER;
    }

    public static String getUser_Name() {
        return user_Name;
    }

    public static void setUser_Name(String user_Name) {
        Jdbc.user_Name = user_Name;
    }

    public static String getUser_PSW() {
        return user_PSW;
    }

    public static void setUser_PSW(String user_PSW) {
        Jdbc.user_PSW = user_PSW;
    }
}
